package org.aplikator.server.descriptor;

import org.aplikator.client.local.command.ExecuteFunction;
import org.aplikator.client.local.command.ListEntities;
import org.aplikator.client.shared.descriptor.MenuDTO;
import org.aplikator.server.Context;

import java.util.ArrayList;
import java.util.List;

public class Menu extends ServerDescriptorBase {

    private List<Action> actions = new ArrayList<Action>();

    public Menu(String name) {
        super(name);
        this.setLocalizationKey(name);
    }

    public Menu addAction(Action action) {
        actions.add(action);
        return this;
    }

    public List<Action> getActions() {
        return actions;
    }

    public MenuDTO getMenuDTO(Context ctx) {
        MenuDTO retval = new MenuDTO(this.getId(), this.getLocalizedName(ctx));
        for (Action a : actions) {
            retval.addAction(a.getActionDTO(ctx));
        }
        return retval;
    }

    public Menu addView(View view) {
        return addAction(new Action(view.getId(), view.getLocalizationKey(), new ListEntities(view.getId()).getPlace().getToken()));
    }

    public Menu addFunction(Function function) {
        return addAction(new Action(function.getId(), function.getLocalizationKey(), new ExecuteFunction(function.getId()).getPlace().getToken()));
    }

}
